package com.wisemoresystem.seal.action_app.useapply;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

import org.ne.framework.data.util.ChkTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wisemoresystem.seal.base.action.ProjBaseAction;
import com.wisemoresystem.seal.config.Config;
import com.wisemoresystem.seal.model.t_file;
import com.wisemoresystem.seal.model.t_seal_use_apply;
import com.wisemoresystem.seal.model.t_seal_use_apply_pp;
import com.wisemoresystem.seal.service.UseApplyService;
import com.wisemoresystem.seal.util.DateTools;
import com.wisemoresystem.seal.util.file.File64CodeTools;
import com.wisemoresystem.seal.util.json.JsonTools;
import com.wisemoresystem.seal.web.WebTools;

/**
 * .net调用获取员工,机构信息json
 */

@Controller("ixAppApplyAction")
@Scope("prototype")
@RequestMapping("/ix/net/apply")
public class AppApplyAction extends ProjBaseAction {

	@Autowired
	private UseApplyService uAService;

	/**
	 * 需要盖章的apply list json
	 * 
	 * @return
	 */
	@RequestMapping("/list_json")
	@ResponseBody
	public String list() {

		List<Map<String, Object>> applyList = uAService.selectUseApplyList();

		return JsonTools.toJson(applyList);
	}

	/**
	 * 需要盖章的apply list json
	 * 
	 * @return
	 */
	@RequestMapping("/end")
	@ResponseBody
	public String end(String apply_id) {

		t_seal_use_apply apply = oc.findObjById(apply_id,
				t_seal_use_apply.class);
		apply.setSeal_status("已盖章");
		apply.setVerify_status("待审核");
		oc.updateObj(apply);

		// result.put("result", "true");
		// return JsonTools.toJson(result);
		return "true";
	}

	/**
	 * 盖章上传接口
	 * 
	 * @return
	 */
	@RequestMapping("/affix")
	@ResponseBody
	public String affix(@RequestBody String json) throws IOException,
			ParseException {
		// System.err.println("===========>" + json);
		Map<String, Object> map = JsonTools.jsonStrToMap(json);
		if (ChkTools.isNull(map)) {
			return "false";
		}

		String apply_id = (String) map.get("id");
		String seal_code = (String) map.get("seal_code");
		String mac_code = (String) map.get("mac_code");
		String file_id_f = (String) map.get("file_id_before");
		String file_id_b = (String) map.get("file_id");

		t_file file_f = save64to_t_file(file_id_f);
		t_file file_b = save64to_t_file(file_id_b);

		t_seal_use_apply_pp pp = new t_seal_use_apply_pp();
		pp.setApply_id(apply_id);
		pp.setBank_id(WebTools.getCurrentBankId());
		pp.setFile_id_b(file_b.get_id());
		pp.setFile_id_f(file_f.get_id());
		pp.setMac_id(mac_code);
		pp.setSeal_id(seal_code);
		pp.setUser_id(WebTools.getCurrentUserId());
		int vorder = uAService.selectMaxPPOrder(apply_id);
		pp.setVorder(++vorder);
		oc.saveObj(pp);

		return "true";
	}

	public t_file save64to_t_file(String pic) {
		int year = DateTools.getYear();
		int month = DateTools.getMonth();
		String folder = year + File.separator + month;
		String folder_url = year + "/" + month;
		String filename = System.currentTimeMillis() + ".jpeg";
		// 保存文件
		t_file tf = new t_file();
		tf.setFile_name(filename);
		tf.setNew_file_name(filename);
		tf.setFolder(folder);
		tf.setFolder_url(folder_url);
		tf.setContent_type("image/jpeg");

		File newFile = new File(Config.DATA_PATH + File.separator + folder
				+ File.separator + tf.getNew_file_name());
		File64CodeTools.writeFiles(newFile, pic);

		oc.saveObj(tf);

		return tf;
	}

}
